package com.itfreer.data.metadata;

import com.itfreer.data.SysDatabaseHelper;
import com.itfreer.data.dictionary.DictionaryManage;
import com.itfreer.data.metadata.entity.MetadataEntity;
import com.itfreer.data.metadata.entity.MetadataValue;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义元数据服务
 * 用于属性数据转换
 */
public class MetadataService {

    public static final MetadataService instance = new MetadataService();
    private MetadataService() {
    }

    /**
     * 查询元数据
     * @param metadata
     * @return
     * @throws SQLException
     */
    public MetadataEntity getMetadata(String metadata, String fieldName) throws SQLException {
        if(metadata==null || metadata.equals("")){
            return null;
        }
        Dao<MetadataEntity, String> dao = SysDatabaseHelper.getInstance().getDao(MetadataEntity.class);
        Map<String, Object> where = new HashMap<>();
        where.put("mid", metadata);
        where.put("name", fieldName);
        List<MetadataEntity> list = dao.queryForFieldValues(where);
        if(list==null || list.size()<=0){
            return null;
        }else{
            return list.get(0);
        }
    }

    /**
     * 查询元数据
     * @param metadata
     * @return
     * @throws SQLException
     */
    public List<MetadataEntity> getMetadata(String metadata) throws SQLException {
        if(metadata==null || metadata.equals("")){
            return null;
        }
        Dao<MetadataEntity, String> dao = SysDatabaseHelper.getInstance().getDao(MetadataEntity.class);
        List<MetadataEntity> list = dao.queryForEq("mid", metadata);
        list.sort(new Comparator<MetadataEntity>() {
            @Override
            public int compare(MetadataEntity o1, MetadataEntity o2) {
                if(o1.getOrder()==null || o2.getOrder()==null){
                    return 0;
                }
                return o1.getOrder().compareTo(o2.getOrder());
            }
        });
        return list;
    }

    /**
     * 属性数据转换
     * @param metadata
     * @param attributes
     * @return
     */
    public Map<String, MetadataValue> convert(String metadata, Map<String, Object> attributes) throws SQLException {
        if(metadata==null || metadata.equals("")){
            return convert(attributes);
        }

        if(attributes==null || attributes.size()<=0){
            return convert(attributes);
        }

        List<MetadataEntity> metadatas = getMetadata(metadata);
        if(metadatas==null){
            return convert(attributes);
        }

        Map<String, MetadataValue> result = new LinkedHashMap<>();
        for(MetadataEntity item : metadatas){
            String fieldName = item.getName();
            fieldName = fieldName.toUpperCase();
            Object fieldValue = null;
            if(attributes.containsKey(fieldName)){
                fieldValue = attributes.get(fieldName);
            }
            String dictionaryValue = null;
            if(item.getDictionaryName()!=null && !item.getDictionaryName().equals("") && fieldValue!=null){
                dictionaryValue = DictionaryManage.instance.getText(item.getDictionaryName(), fieldValue.toString());
            }
            result.put(fieldName, new MetadataValue(fieldValue, item, dictionaryValue));
        }
        return result;
    }

    private Map<String, MetadataValue> convert(Map<String, Object> attributes){
        if(attributes==null){
            return null;
        }

        Map<String, MetadataValue> result = new LinkedHashMap<>();
        for(String key : attributes.keySet()){
            result.put(key, new MetadataValue(attributes.get(key),null,null));
        }
        return result;
    }

}
